/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package shell;

import games.FourInARowState;
import games.FourInARowState.FourInARowMove;

/**
 *
 * @author Mikael
 */
public class FourInARowGame extends AbstractGame<FourInARowMove, FourInARowState> {

    public FourInARowGame(String[] args) {
		super(FourInARowState.class, 10, args);
	}

	@Override
    protected FourInARowState createState(String name) {
        return new FourInARowState();
    }

	@Override
	protected void displayGUI(FourInARowState game) {
		;
	}
	
    @Override
    protected String render(FourInARowState game) {
        StringBuilder buffer = new StringBuilder();
        for (int y = 5; y >= 0; y--) {
            for (int x = 0; x < 7; x++) {
                Boolean cell = game.cell(x, y);
                char c = cell == null ? ' ' : cell ? '0' : 'X';
                buffer.append(c);
                if (x < 6) {
                    buffer.append('|');
                }
            }
            buffer.append("\n");
        }
        return buffer.toString();
    }

    @Override
    protected FourInARowMove parseMove(FourInARowState state, String command) {
        FourInARowMove result;
        try {
            int x = Integer.parseInt(command);
            
            for (int y = 0; y < 6; y++) {
            	result = new FourInARowMove(state.player(), x, y);
            	if(state.cellIsFree(result)) {
            		return result;
            	}
            }
            return null;
        } catch (NumberFormatException e) {
            result = null;
        }
        return null;
    }

	/**
	 * @param args the command line arguments
	 */
	public static void main(String[] args) {
	    new FourInARowGame(args).play();
	}

	@Override
	protected FourInARowMove waitForMove(FourInARowState state) {
		return null;
	}
}
